package cn.zb.zbrhback.auth.service.impl;

import cn.zb.zbrhback.auth.domain.Role;
import cn.zb.zbrhback.auth.domain.RoleQuery;
import cn.zb.zbrhback.auth.mapper.RoleMapper;
import cn.zb.zbrhback.auth.service.IRoleService;
import cn.zb.zbrhback.basic.service.impl.BaseServiceImpl;
import cn.zb.zbrhback.basic.util.MenuDTO;
import cn.zb.zbrhback.basic.util.PageResult;
import cn.zb.zbrhback.basic.util.PermissionDTO;
import cn.zb.zbrhback.basic.util.RoleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<String> getSnByRoleId(Long roleId) {
        return roleMapper.getSnByRoleId(roleId);
    }

    @Override
    @Transactional
    public void saveRolePermission(PermissionDTO permissionDTO) {
        roleMapper.deleteById(permissionDTO.getRoleId());
        System.out.println("保存角色权限关系"+permissionDTO);
        if(permissionDTO.getPermissionSns().size() > 0){
            roleMapper.saveRolePermission(permissionDTO);
        }
    }

    @Override
    public List<Long> getRoleMenuById(Long id) {
        return roleMapper.getRoleMenuById(id);
    }

    @Override
    @Transactional
    public void saveMenu(MenuDTO menuDTO) {
        roleMapper.deleteMenuById(menuDTO.getId());
        System.out.println("保存角色菜单关系"+menuDTO);
        if(menuDTO.getAllMenu().size() > 0){
            roleMapper.saveMenu(menuDTO);
        }
    }

    @Override
    public List<Long> getAllRoleId() {
        return roleMapper.getAllRoleId();
    }

    @Override
    public List<Long> getSnById(Long id) {
        return roleMapper.getSnById(id);
    }

    @Override
    public void saveRole(RoleDTO roleDTO) {
        roleMapper.deleteRoleById(roleDTO.getId());
        System.out.println("保存角色菜单关系"+roleDTO);
        roleMapper.saveRole(roleDTO);
    }
}
